package com.uzai.console.service.zombiefanpassive.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.xml.bind.v2.model.core.ID;
import com.uzai.common.entity.User;
import com.uzai.common.enums.*;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.msg.MsgDto;
import com.uzai.common.msg.NoticeMsgDto;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.ResponseMsg;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.constant.ES.ElasticsearchUtilGroupMsg;
import com.uzai.console.dto.order.OrderQuery;
import com.uzai.console.dto.user.UserQuery;
import com.uzai.console.dto.zombiefanpassive.zombiefanpassive.ZombieFanPassiveIdDto;
import com.uzai.console.dto.zombiefanpassive.zombiefanpassive.ZombieFanPassiveIdListDto;
import com.uzai.console.dto.zombiefanpassive.zombiefanpassive.ZombieFanPassiveQuery;
import com.uzai.console.entity.Device;
import com.uzai.console.entity.ZombieFanPassive;
import com.uzai.console.entity.ZombieFanPassiveTask;
import com.uzai.console.entity.ZombieFanPassiveTaskSendInfo;
import com.uzai.console.jsonvo.zombiefanpassive.ZombieFanPassiveSendPara;
import com.uzai.console.mapper.ZombieFanPassiveMapper;
import com.uzai.console.mapper.ZombieFanPassiveTaskMapper;
import com.uzai.console.mapper.ZombieFanPassiveTaskSendInfoMapper;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.merchant.MerchantService;
import com.uzai.console.service.order.OrderOptService;
import com.uzai.console.service.rocketmq.NoticeMsgPushProducerService;
import com.uzai.console.service.user.UserService;
import com.uzai.console.service.zombiefanpassive.ZombieFanPassiveService;
import com.uzai.console.vo.order.BaseOrderVo;
import com.uzai.console.vo.takebal.usertakebal.UserTakeBalReportVo;
import com.uzai.console.vo.user.UserInfoVo;
import com.uzai.console.vo.zombiefanpassive.zombiefanpassive.ZombieFanPassiveVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 被动僵尸粉任务
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class ZombieFanPassiveServiceImpl implements ZombieFanPassiveService {

    Logger logger = LoggerFactory.getLogger(ZombieFanPassiveServiceImpl.class);

    @Autowired
    private ZombieFanPassiveMapper zombieFanPassiveMapper;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private ElasticsearchUtilGroupMsg elasticsearchUtilGroupMsg;
    @Autowired
    private NoticeMsgPushProducerService noticeMsgPushProducerService;
    @Autowired
    private UserService userService;
    @Autowired
    private ZombieFanPassiveTaskMapper zombieFanPassiveTaskMapper;
    @Autowired
    private OrderOptService orderOptService;
    @Autowired
    private ZombieFanPassiveTaskSendInfoMapper zombieFanPassiveTaskSendInfoMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询被动僵尸粉任务列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param zombieFanPassiveQuery
     * @return null
     */
    public Page<ZombieFanPassiveVo> findZombieFanPassiveList(ZombieFanPassiveQuery zombieFanPassiveQuery){

        Integer now = DateUtil.getNowTime();

        //新建返回到页面的LIST对象
        List<ZombieFanPassiveVo> zombieFanPassiveInfoVoList = new ArrayList<>();

        //执行分页查询对象
        Page<ZombieFanPassive> page = new Page<>();
        zombieFanPassiveQuery.convert(page);
        List<ZombieFanPassive> zombieFanPassiveList = zombieFanPassiveMapper.findByList(page, zombieFanPassiveQuery);
        if(zombieFanPassiveList != null){
            for (ZombieFanPassive zombieFanPassive : zombieFanPassiveList){
                ZombieFanPassiveVo zombieFanPassiveInfoVo = new ZombieFanPassiveVo();
                BeanUtils.copyProperties(zombieFanPassive, zombieFanPassiveInfoVo);

                //查询群发类型名称
                if(StringUtils.isNotBlank(zombieFanPassive.getDeviceType())){
                    DeviceTypeEnum deviceTypeEnum = DeviceTypeEnum.getByKey(zombieFanPassive.getDeviceType());
                    if(deviceTypeEnum != null){
                        zombieFanPassiveInfoVo.setDeviceTypeName(deviceTypeEnum.getRemark());
                    }
                }

                //机器人信息
                if(StringUtils.isNotBlank(zombieFanPassive.getDeviceType())){
                    Device device = deviceService.findDeviceInfoByDeviceUniqueId(zombieFanPassive.getDeviceUniqueId(), zombieFanPassive.getDeviceType(), zombieFanPassive.getMerId());
                    if(device != null){
                        zombieFanPassiveInfoVo.setDeviceId(device.getDeviceId());
                        zombieFanPassiveInfoVo.setDeviceName(device.getDeviceName());
                        zombieFanPassiveInfoVo.setDeviceRemark(device.getDeviceRemark());
                    }

                    Device devicePass = deviceService.findDeviceInfoByDeviceUniqueId(zombieFanPassive.getDeviceUniqueIdPass(), zombieFanPassive.getDeviceType(), zombieFanPassive.getMerId());
                    if(devicePass != null){
                        zombieFanPassiveInfoVo.setDeviceIdPass(devicePass.getDeviceId());
                        zombieFanPassiveInfoVo.setDeviceNamePass(devicePass.getDeviceName());
                        zombieFanPassiveInfoVo.setDeviceRemarkPass(devicePass.getDeviceRemark());
                    }
                }

                zombieFanPassiveInfoVoList.add(zombieFanPassiveInfoVo);

            }
        }
        //返回到页面分页对象
        Page<ZombieFanPassiveVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(zombieFanPassiveInfoVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询被动僵尸粉任务详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param zombieFanPassiveIdDto
     * @return null
     */
    public ZombieFanPassiveVo findZombieFanPassive(ZombieFanPassiveIdDto zombieFanPassiveIdDto){
        //返回到页面的VO对象
        ZombieFanPassiveVo zombieFanPassiveInfoVo = new ZombieFanPassiveVo();

        if(zombieFanPassiveIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        ZombieFanPassive zombieFanPassive = zombieFanPassiveMapper.selectById(zombieFanPassiveIdDto.getId(), zombieFanPassiveIdDto.getMerId());

        if(zombieFanPassive == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

        //将查询到的数据复制到页面VO中
        BeanUtils.copyProperties(zombieFanPassive, zombieFanPassiveInfoVo);

        //查询群发类型名称
        if(StringUtils.isNotBlank(zombieFanPassive.getDeviceType())){
            DeviceTypeEnum deviceTypeEnum = DeviceTypeEnum.getByKey(zombieFanPassive.getDeviceType());
            if(deviceTypeEnum != null){
                zombieFanPassiveInfoVo.setDeviceTypeName(deviceTypeEnum.getRemark());
            }
        }

        //机器人信息
        if(StringUtils.isNotBlank(zombieFanPassive.getDeviceType())){
            Device device = deviceService.findDeviceInfoByDeviceUniqueId(zombieFanPassive.getDeviceUniqueId(), zombieFanPassive.getDeviceType(), zombieFanPassive.getMerId());
            if(device != null){
                zombieFanPassiveInfoVo.setDeviceId(device.getDeviceId());
                zombieFanPassiveInfoVo.setDeviceName(device.getDeviceName());
                zombieFanPassiveInfoVo.setDeviceRemark(device.getDeviceRemark());
            }

            Device devicePass = deviceService.findDeviceInfoByDeviceUniqueId(zombieFanPassive.getDeviceUniqueIdPass(), zombieFanPassive.getDeviceType(), zombieFanPassive.getMerId());
            if(devicePass != null){
                zombieFanPassiveInfoVo.setDeviceIdPass(devicePass.getDeviceId());
                zombieFanPassiveInfoVo.setDeviceNamePass(devicePass.getDeviceName());
                zombieFanPassiveInfoVo.setDeviceRemarkPass(devicePass.getDeviceRemark());
            }
        }

        return  zombieFanPassiveInfoVo;
    }

    /**
     * 批量删除被动僵尸粉任务
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param zombieFanPassiveIdListDto
     * @return null
     */
    public void batchDeleteZombieFanPassive(ZombieFanPassiveIdListDto zombieFanPassiveIdListDto){
        List<Long> idList = zombieFanPassiveIdListDto.getIdList();
        if(idList == null || idList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择僵尸粉");
        }

        for (Long id : zombieFanPassiveIdListDto.getIdList()) {
            zombieFanPassiveMapper.deleteById(id, zombieFanPassiveIdListDto.getMerId());
        }
    }

    /**
     * 被动僵尸粉发送添加消息
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param zombieFanPassiveIdDto
     * @return null
     */
    public void sendMsg(ZombieFanPassiveIdDto zombieFanPassiveIdDto){
        if(zombieFanPassiveIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        ZombieFanPassive zombieFanPassive = zombieFanPassiveMapper.selectById(zombieFanPassiveIdDto.getId(), zombieFanPassiveIdDto.getMerId());
        if(zombieFanPassive == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该僵尸粉不存在");
        }

        //查找最新的被动僵尸粉任务
        ZombieFanPassiveTask zombieFanPassiveTask = zombieFanPassiveTaskMapper.selectByDeviceType(zombieFanPassive.getDeviceType(), zombieFanPassive.getMerId());

        //发送添加消息
        dealZombieFanPassiveSendMsg(zombieFanPassiveTask, zombieFanPassive);
    }

    /**
     * 处理僵尸粉发送添加消息
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param zombieFanPassive
     * @return null
     */
    public void dealZombieFanPassiveSendMsg(ZombieFanPassiveTask zombieFanPassiveTask, ZombieFanPassive zombieFanPassive){

        //获取发送消息
        String sendMsg = getSendMsg(zombieFanPassiveTask, zombieFanPassive);
        if(StringUtils.isBlank(sendMsg)){
            zombieFanPassive.setStatus(4); //未满足条件
            zombieFanPassive.setUpdateTime(DateUtil.getNowTime());
            zombieFanPassiveMapper.updateByIdSelective(zombieFanPassive);

            logger.error("被动僵尸粉发送添加好友未找到发送消息");
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "未找到满足发送条件的消息");
        }

        //指定消息开头指令
        //sendMsg = "VERIFY|" + sendMsg;

        //查找被动僵尸粉发添加好友机器人
        Long sendDeviceUniqueId = getSendDeviceUniqueId(zombieFanPassiveTask);
        if(sendDeviceUniqueId == null){
            logger.error("被动僵尸粉发送添加好友未找到合适机器人唯一id");
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "未找到合适机器人");
        }

        //查找发送方机器人
        Device device_from = deviceService.findDeviceInfoByDeviceUniqueId(zombieFanPassive.getDeviceUniqueId(), zombieFanPassive.getDeviceType(), zombieFanPassive.getMerId());
        if(device_from == null){
            logger.error("被动僵尸粉所在机器人不存在");
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "被动僵尸粉所在机器人不存在");
        }
        Device device_to = deviceService.findDeviceInfoByDeviceUniqueId(sendDeviceUniqueId, zombieFanPassive.getDeviceType(), zombieFanPassive.getMerId());
        if(device_to == null){
            logger.error("被动僵尸粉发送添加好友未找到接收消息机器人");
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "配置发送的机器人已经不存在");
        }

        //1-发送消息保存到缓存；2-等待1秒钟；3-发送名片
//        MsgDto<NoticeMsgDto> msgDto_text = new MsgDto<>();
//        NoticeMsgDto  noticeMsgDto_text = new NoticeMsgDto();
//        msgDto_text.setContent(noticeMsgDto_text);
//        msgDto_text.setId(IdWorker.getId());
//        msgDto_text.setDeviceType(device_from.getRecvmsgType());
//        msgDto_text.setDeviceUniqueId(zombieFanPassive.getDeviceUniqueId());
//        msgDto_text.setMerId(zombieFanPassive.getMerId());
//        msgDto_text.setMsgType(MessageFuncEnum.SEND_CHAT_MESSAGE.getValue());
//        noticeMsgDto_text.setContent(sendMsg);
//        noticeMsgDto_text.setBizUserId(device_to.getDeviceId());
//        noticeMsgDto_text.setContentType(MessageTypeEnum.MT_TEXT.getValue());
//        noticeMsgDto_text.setGroupId(null);
//        noticeMsgDto_text.setNickName(device_to.getDeviceName());
//        noticeMsgDto_text.setMsgSource(1);
//        noticeMsgDto_text.setDelay(0);
//        noticeMsgDto_text.setMsgBizType(KefuSendMsgEnum.UNSOLICITEDMSG.getKey());
//        noticeMsgPushProducerService.sendMessage(msgDto_text);
//
//        try {
//            Thread.sleep(3000); //停顿3秒，在发卡片
//        }catch (Exception e){
//
//        }

        //发送被动僵尸粉添加好友验证消息key
        String addZombieFanpassiveSendMsg = String.format(RedisCacheKeyEnum.ADD_ZOMBIE_FANPASSIVE_SEND_MSG.getKey(), device_to.getId());
        redisTemplate.opsForValue().set(addZombieFanpassiveSendMsg, sendMsg);

        //发送名片
        MsgDto<NoticeMsgDto> msgDto_card = new MsgDto<>();
        NoticeMsgDto  noticeMsgDto_card = new NoticeMsgDto();
        msgDto_card.setContent(noticeMsgDto_card);
        msgDto_card.setId(IdWorker.getId());
        msgDto_card.setDeviceType(device_from.getRecvmsgType());
        msgDto_card.setDeviceUniqueId(zombieFanPassive.getDeviceUniqueId());
        msgDto_card.setMerId(zombieFanPassive.getMerId());
        msgDto_card.setMsgType(MessageFuncEnum.SEND_CHAT_MESSAGE.getValue());
        msgDto_card.setCsMsg(true); //客服消息不走悠聊系统
        noticeMsgDto_card.setContent(zombieFanPassive.getWxid()); //指定的僵尸粉的微信id
        noticeMsgDto_card.setBizUserId(device_to.getDeviceId());
        noticeMsgDto_card.setContentType(MessageTypeEnum.MT_NAME_CARD.getValue());
        noticeMsgDto_card.setGroupId(null);
        noticeMsgDto_card.setNickName(device_to.getDeviceName());
        noticeMsgDto_card.setMsgSource(1);
        noticeMsgDto_card.setDelay(0);
        noticeMsgDto_card.setMsgBizType(KefuSendMsgEnum.UNSOLICITEDMSG.getKey());
        noticeMsgPushProducerService.sendMessage(msgDto_card);

        //添加发送记录
        int now = DateUtil.getNowTime();
        ZombieFanPassiveTaskSendInfo zombieFanPassiveTaskSendInfo = new ZombieFanPassiveTaskSendInfo();
        zombieFanPassiveTaskSendInfo.setId(IdWorker.getId());
        zombieFanPassiveTaskSendInfo.setMerId(zombieFanPassiveTask.getMerId());
        zombieFanPassiveTaskSendInfo.setTaskId(zombieFanPassiveTask.getId());
        zombieFanPassiveTaskSendInfo.setDeviceType(zombieFanPassiveTask.getDeviceType());
        zombieFanPassiveTaskSendInfo.setDeviceUniqueId(device_to.getId());
        zombieFanPassiveTaskSendInfo.setWxid(device_to.getDeviceId());
        zombieFanPassiveTaskSendInfo.setNick(device_to.getDeviceName());
        zombieFanPassiveTaskSendInfo.setSendMsg(sendMsg);
        zombieFanPassiveTaskSendInfo.setSendTime(now);
        zombieFanPassiveTaskSendInfo.setCreateTime(now);
        zombieFanPassiveTaskSendInfo.setUpdateTime(now);
        zombieFanPassiveTaskSendInfoMapper.insert(zombieFanPassiveTaskSendInfo);

        //修改发送时间
        zombieFanPassiveTask.setSendTime(now);
        zombieFanPassiveTaskMapper.updateSendTimeById(zombieFanPassiveTask);

        //修改被动僵尸粉状态
        zombieFanPassive.setDeviceUniqueIdPass(sendDeviceUniqueId);
        zombieFanPassive.setStatus(1);
        zombieFanPassive.setSendTime(now);
        zombieFanPassive.setUpdateTime(now);
        zombieFanPassiveMapper.updateByIdSelective(zombieFanPassive);
    }

    /**
     * 获取被动僵尸粉添加时发送消息
     * @author jitwxs
     * @date 2023/12/11 10:21
     * @param zombieFanPassiveTask
     * @return boolean
     */
    public String getSendMsg(ZombieFanPassiveTask zombieFanPassiveTask, ZombieFanPassive zombieFanPassive){

        //判断删除会员条件，满足是否有积分、有余额，是否有提现记录，是否有未结算订单，是否有多个机器人会员
        String sendMsg = null;
        String sendPara = zombieFanPassiveTask.getSendPara();
        if(StringUtils.isBlank(sendPara)){
            return sendMsg;
        }

        ZombieFanPassiveSendPara zombieFanPassiveSendPara = JSONObject.parseObject(sendPara, ZombieFanPassiveSendPara.class);

        //选择会员余额验证
        if(Tools.getInteger(zombieFanPassiveSendPara.getUserBalFlag()).intValue() == 1){
            Double userBalMin = Tools.getDouble(zombieFanPassiveSendPara.getUserBalMin());
            Double userBalMax = Tools.getDouble(zombieFanPassiveSendPara.getUserBalMax());
            //验证余额
            User user = userService.findUserInfoByWxid(zombieFanPassive.getMerId(), zombieFanPassive.getWxid(), zombieFanPassive.getDeviceType());
            if(user != null){
                Double userBal = Tools.getDouble(user.getBalance());
                if(userBal >= userBalMin && userBal <= userBalMax){
                    sendMsg = zombieFanPassiveSendPara.getUserBalMsg();
                    sendMsg = sendMsg.replace("[昵称]", zombieFanPassive.getNickName())
                                     .replace("[余额]", String.valueOf(userBal));
                    return sendMsg;
                }
            }
        }else if(Tools.getInteger(zombieFanPassiveSendPara.getUserIntegralFlag()).intValue() == 1){ //会员积分验证
            Integer userIntegralMin = Tools.getInteger(zombieFanPassiveSendPara.getUserIntegralMin());
            Integer userIntegralMax = Tools.getInteger(zombieFanPassiveSendPara.getUserIntegralMax());
            //验证余额
            User user = userService.findUserInfoByWxid(zombieFanPassive.getMerId(), zombieFanPassive.getWxid(), zombieFanPassive.getDeviceType());
            if(user != null){
                Integer userIntegral = Tools.getInteger(user.getIntegral());
                if(userIntegral >= userIntegralMin && userIntegral <= userIntegralMax){
                    sendMsg = zombieFanPassiveSendPara.getUserIntegralMsg();
                    sendMsg = sendMsg.replace("[昵称]", zombieFanPassive.getNickName())
                            .replace("[积分]", String.valueOf(userIntegral));
                    return sendMsg;
                }
            }
        }else if(Tools.getInteger(zombieFanPassiveSendPara.getSettleOrderFlag()).intValue() == 1){ //有结算订单
            //验证余额
            User user = userService.findUserInfoByWxid(zombieFanPassive.getMerId(), zombieFanPassive.getWxid(), zombieFanPassive.getDeviceType());
            if(user != null){
                //淘宝是否有付款和维权中的订单
                OrderQuery orderQuery_status = new OrderQuery();
                orderQuery_status.setMerId(zombieFanPassive.getMerId());
                orderQuery_status.setUserId(user.getId());
                orderQuery_status.setStatus(OrderStatusEnum.STATUS_SETTLED.getValue());
                //是否有淘宝订单
                Page<BaseOrderVo> baseOrderList_tb_status = orderOptService.findBaseOrderList(orderQuery_status, PlatformTypeEnum.PT_TAOBAO_VALUE);
                if(baseOrderList_tb_status != null && baseOrderList_tb_status.getTotal() > 0){
                    sendMsg = zombieFanPassiveSendPara.getSettleOrderMsg();
                    sendMsg = sendMsg.replace("[昵称]", zombieFanPassive.getNickName());
                    return sendMsg;
                }
                //是否有京东订单
                Page<BaseOrderVo> baseOrderList_jd_status = orderOptService.findBaseOrderList(orderQuery_status, PlatformTypeEnum.PT_JD_VALUE);
                if(baseOrderList_jd_status != null && baseOrderList_jd_status.getTotal() > 0){
                    sendMsg = zombieFanPassiveSendPara.getSettleOrderMsg();
                    sendMsg = sendMsg.replace("[昵称]", zombieFanPassive.getNickName());
                    return sendMsg;
                }
                //是否有拼多多订单
                Page<BaseOrderVo> baseOrderList_pdd_status = orderOptService.findBaseOrderList(orderQuery_status, PlatformTypeEnum.PT_PDD_VALUE);
                if(baseOrderList_pdd_status != null && baseOrderList_pdd_status.getTotal() > 0){
                    sendMsg = zombieFanPassiveSendPara.getSettleOrderMsg();
                    sendMsg = sendMsg.replace("[昵称]", zombieFanPassive.getNickName());
                    return sendMsg;
                }
                //是否有唯品会订单
                Page<BaseOrderVo> baseOrderList_vph_status = orderOptService.findBaseOrderList(orderQuery_status, PlatformTypeEnum.PT_VPH_VALUE);
                if(baseOrderList_vph_status != null && baseOrderList_vph_status.getTotal() > 0){
                    sendMsg = zombieFanPassiveSendPara.getSettleOrderMsg();
                    sendMsg = sendMsg.replace("[昵称]", zombieFanPassive.getNickName());
                    return sendMsg;
                }
                //是否有美团订单
                Page<BaseOrderVo> baseOrderList_mt_status = orderOptService.findBaseOrderList(orderQuery_status, PlatformTypeEnum.PT_MT_LM_VALUE);
                if(baseOrderList_mt_status != null && baseOrderList_mt_status.getTotal() > 0){
                    sendMsg = zombieFanPassiveSendPara.getSettleOrderMsg();
                    sendMsg = sendMsg.replace("[昵称]", zombieFanPassive.getNickName());
                    return sendMsg;
                }
                //是否有饿了么订单
                Page<BaseOrderVo> baseOrderList_elm_status = orderOptService.findBaseOrderList(orderQuery_status, PlatformTypeEnum.PT_ELM_VALUE);
                if(baseOrderList_elm_status != null && baseOrderList_elm_status.getTotal() > 0){
                    sendMsg = zombieFanPassiveSendPara.getSettleOrderMsg();
                    sendMsg = sendMsg.replace("[昵称]", zombieFanPassive.getNickName());
                    return sendMsg;
                }
                //是否有抖音订单
                Page<BaseOrderVo> baseOrderList_dy_status = orderOptService.findBaseOrderList(orderQuery_status, PlatformTypeEnum.PT_DY_VALUE);
                if(baseOrderList_dy_status != null && baseOrderList_dy_status.getTotal() > 0){
                    sendMsg = zombieFanPassiveSendPara.getSettleOrderMsg();
                    sendMsg = sendMsg.replace("[昵称]", zombieFanPassive.getNickName());
                    return sendMsg;
                }
                //是否有快手订单
                Page<BaseOrderVo> baseOrderList_ks_status = orderOptService.findBaseOrderList(orderQuery_status, PlatformTypeEnum.PT_KS_VALUE);
                if(baseOrderList_ks_status != null && baseOrderList_ks_status.getTotal() > 0){
                    sendMsg = zombieFanPassiveSendPara.getSettleOrderMsg();
                    sendMsg = sendMsg.replace("[昵称]", zombieFanPassive.getNickName());
                    return sendMsg;
                }
            }
        }else if(Tools.getInteger(zombieFanPassiveSendPara.getHaveSubParentFlag()).intValue() == 1){ //有下级
            //验证余额
            User user = userService.findUserInfoByWxid(zombieFanPassive.getMerId(), zombieFanPassive.getWxid(), zombieFanPassive.getDeviceType());
            if(user != null){
                //查询下级列表
                UserQuery userQuery = new UserQuery();
                userQuery.setMerId(zombieFanPassive.getMerId());
                userQuery.setParentId(user.getId());
                userQuery.setCurrent(1);
                userQuery.setSize(5); //查询下级列表
                Page<UserInfoVo> userPage = userService.findUserPage(userQuery);
                if(userPage != null && userPage.getTotal() > 0){ //有下级
                    sendMsg = zombieFanPassiveSendPara.getHaveSubParentMsg();
                    sendMsg = sendMsg.replace("[昵称]", zombieFanPassive.getNickName());
                    return sendMsg;
                }
            }
        }
        return sendMsg;
    }

    /**
     * 获取被动僵尸粉添加时发送消息
     * @author jitwxs
     * @date 2023/12/11 10:21
     * @param zombieFanPassiveTask
     * @return boolean
     */
    public Long getSendDeviceUniqueId(ZombieFanPassiveTask zombieFanPassiveTask){
        //发送消息的得机器人列表
        String deviceUniqueIds = zombieFanPassiveTask.getDeviceUniqueIds();
        //每天发送上限数
        Integer sendDayLimit = zombieFanPassiveTask.getSendDayLimit();
        if(StringUtils.isBlank(deviceUniqueIds)){
            return null;
        }

        List<Long> deviceUniqueIdList = JSONObject.parseArray(deviceUniqueIds, Long.class);
        if(deviceUniqueIdList == null || deviceUniqueIdList.size() == 0){
            return null;
        }

        for (Long deviceUniqueId : deviceUniqueIdList) {
            //查询到该机器人当前发送数
            int sendCount = zombieFanPassiveTaskSendInfoMapper.selectCountByDeviceUniqueId(deviceUniqueId, zombieFanPassiveTask.getMerId());
            //如果该机器人当前发送数少于每天发送上限数，则表示该机器人还可以添加，否则查找下一个机器人
            if(sendCount < sendDayLimit){
                return deviceUniqueId;
            }
        }

        return null;

    }



}
